﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeBulid.Winform
{
    public partial class Form1 : Form
    {
        private readonly List<CodeBuild.Models.Enum.Operation> _listOperations;
        public Form1()
        {
            InitializeComponent();
            _listOperations = new List<CodeBuild.Models.Enum.Operation>();
        }

        private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
        {
            cbxInsert.Checked = checkBoxAll.Checked;
            cbxUpdate.Checked = checkBoxAll.Checked;
            cbxDelete.Checked = checkBoxAll.Checked;
            cbxSelect.Checked = checkBoxAll.Checked;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!Check()) return;
            Operation();
            var dialog = new FolderBrowserDialog { Description = @"请选择保存路径" };
            if (dialog.ShowDialog() != DialogResult.OK) return;
            var foldPath = dialog.SelectedPath;

            var model = new CodeBuild.Models.Build()
            {
                ProjectName = tbxProjectName.Text.Trim(),
                NameSpace = tbxNameSpace.Text.Trim(),
                ModelClassName = tbxModelClassName.Text.Trim(),
                ClassName = tbxClassName.Text.Trim(),
                ClassAnnotation = tbxClassAnnotation.Text.Trim(),
                TableName = tbxTableName.Text.Trim(),
                Folder = string.IsNullOrEmpty(tbxFolder.Text.Trim()) ? new string[0] : tbxFolder.Text.Trim().Trim('/').Split('/')
            };
            var arrOperations = _listOperations.ToArray();
            var idalFactory = new CodeBuild.IdalFactoryBuild().Build(model);
            var idal = new CodeBuild.IdalBuild(arrOperations).Build(model);
            var sqlServer = new CodeBuild.SqlServerBuild(arrOperations).Build(model);
            var bll = new CodeBuild.BllBuild(arrOperations).Build(model);
            var controller = new CodeBuild.ControllerBuild(arrOperations).Build(model);

            var path = foldPath + "\\";
            var filePath = path + model.ClassName + "\\";
            var folder = !string.IsNullOrEmpty(tbxFolder.Text.Trim('/')) ? tbxFolder.Text.Trim('/') + "\\" : "";
            Unit.CreateFile.Create(filePath + "IDALFactory\\" + folder + model.ClassName + "Access.cs", idalFactory);
            Unit.CreateFile.Create(filePath + "IDAL\\" + folder + "I" + model.ClassName + ".cs", idal);
            Unit.CreateFile.Create(filePath + "SQLServer\\" + folder + model.ClassName + ".cs", sqlServer);
            Unit.CreateFile.Create(filePath + "BLL\\" + folder + model.ClassName + ".cs", bll);
            Unit.CreateFile.Create(filePath + "Controller\\" + folder + model.ClassName + "Controller.cs", controller);

            if (MessageBox.Show(@"生成代码成功,打开保存目录?", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
            {
                System.Diagnostics.Process.Start(filePath);
            }
        }


        private bool Check()
        {
            if (string.IsNullOrEmpty(tbxProjectName.Text.Trim()))
            {
                MessageBox.Show(@"项目名称不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(tbxNameSpace.Text.Trim()))
            {
                MessageBox.Show(@"命名空间不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (string.IsNullOrEmpty(tbxModelClassName.Text.Trim()))
            {
                MessageBox.Show(@"实体类名不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (string.IsNullOrEmpty(tbxClassName.Text.Trim()))
            {
                MessageBox.Show(@"逻辑类名不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (string.IsNullOrEmpty(tbxClassAnnotation.Text.Trim()))
            {
                MessageBox.Show(@"类注释不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void Operation()
        {
            if (cbxInsert.Checked)
                _listOperations.Add(CodeBuild.Models.Enum.Operation.Insert);
            if (cbxUpdate.Checked)
                _listOperations.Add(CodeBuild.Models.Enum.Operation.Update);
            if (cbxDelete.Checked)
                _listOperations.Add(CodeBuild.Models.Enum.Operation.Delete);
            if (cbxSelect.Checked)
                _listOperations.Add(CodeBuild.Models.Enum.Operation.Select);
        }
    }
}
